package com.shuoyue.ycgk.ui.questionbank.base;

import android.util.TypedValue;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.Option;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.utils.MyRichTextHelper;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;

public class OptionAdapter extends AppBaseQuickAdapter<Option> {
    QuestionSimple questionSimple;
    int textSize = 15;

    public OptionAdapter(@Nullable QuestionSimple questionSimple, boolean isShowDK) {
        super(R.layout.item_option, questionSimple.getQuestionSimpleOptionDTOS());
        this.questionSimple = questionSimple;
        if (questionSimple.getQuestionSimpleOptionDTOS() == null) {
            questionSimple.setQuestionSimpleOptionDTOS(new ArrayList<>());
        }
        for (Option option : questionSimple.getQuestionSimpleOptionDTOS()) {
            if (option.getNo().equals("?")) {
                return;
            }
        }
        //如果没有问号,增加一个不会的选项
        if (isShowDK) {
            questionSimple.getQuestionSimpleOptionDTOS().add(new Option("?", "不会，点我查看解析", false, false));
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, Option item) {
        helper.addOnClickListener(R.id.lay_all);
        helper.setText(R.id.option, item.getNo());
        MyRichTextHelper.showRich(mContext, item.getContent(), helper.getView(R.id.option_value),false);

        ((TextView) helper.getView(R.id.option_value)).setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
//        if (item.getNo().equals("?")) {
//            helper.setVisible(R.id.is_right, false);
//            helper.setBackgroundColor(R.id.lay_all,
//                    ContextCompat.getColor(mContext, R.color.option_bg_gray));
//        } else
        if (questionSimple.isShowRight()) {
            if (item.isRight() && item.isSelect()) {
                //选中正确答案
                helper.setBackgroundColor(R.id.lay_all,
                        ContextCompat.getColor(mContext, R.color.option_bg_green));
                helper.setVisible(R.id.is_right, true);
                helper.setText(R.id.is_right, "√");
                helper.setTextColor(R.id.is_right,
                        ContextCompat.getColor(mContext, R.color.option_text_is_right_green));
            } else if (item.isRight() && !item.isSelect()) {
                //正确答案未选中
                helper.setBackgroundColor(R.id.lay_all,
                        ContextCompat.getColor(mContext, R.color.option_bg_green));
                helper.setVisible(R.id.is_right, false);
            } else if (!item.isRight() && item.isSelect()) {
                //错误选中
                helper.setBackgroundColor(R.id.lay_all,
                        ContextCompat.getColor(mContext, R.color.option_bg_orange));
                helper.setVisible(R.id.is_right, true);
                helper.setTextColor(R.id.is_right,
                        ContextCompat.getColor(mContext, R.color.option_text_is_right_orange));
                helper.setText(R.id.is_right, "×");
            } else if (!item.isRight() && !item.isSelect()) {
                //错误未选中
                helper.setBackgroundColor(R.id.lay_all,
                        ContextCompat.getColor(mContext, R.color.option_bg_gray));
                helper.setVisible(R.id.is_right, false);
            }

        } else if (!questionSimple.isShowRight()) {
            helper.setBackgroundColor(R.id.lay_all, item.isSelect()
                    ? ContextCompat.getColor(mContext, R.color.option_bg_blue)
                    : ContextCompat.getColor(mContext, R.color.option_bg_gray));
            helper.setVisible(R.id.is_right, false);
        } else {
            //恢复原状
            helper.setVisible(R.id.is_right, false);
            helper.setBackgroundColor(R.id.lay_all,
                    ContextCompat.getColor(mContext, R.color.option_bg_gray));
        }
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        notifyDataSetChanged();

    }
}
